package cloud.shyidong.admin.tenant.service;

import cloud.shyidong.core.context.ContextHolder;
import cloud.shyidong.core.exception.BusinessException;
import cloud.shyidong.core.request.LongIdRequest;
import cloud.shyidong.core.service.AbstractService;
import cloud.shyidong.core.util.DateUtils;
import cloud.shyidong.core.util.MD5Utils;
import cloud.shyidong.core.util.ObjectUtils;
import cloud.shyidong.tenant.dao.TenantUserDao;
import cloud.shyidong.tenant.entity.TenantUserEntity;
import cloud.shyidong.tenant.request.TenantUserInsertRequest;
import cloud.shyidong.tenant.request.TenantUserPageRequest;
import cloud.shyidong.tenant.request.TenantUserUpdateRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 租户用户业务处理
 * </p>
 *
 * @author syd
 * @since 2023-09-18 15:34:26
 */
@Service
public class TenantUserService extends AbstractService<TenantUserDao, TenantUserEntity, LongIdRequest, TenantUserInsertRequest, TenantUserUpdateRequest, TenantUserPageRequest> {
    
    @Override
    public void beforeInsert(TenantUserInsertRequest insertRequest) {
        
        if (!insertRequest.getPassword().equals(insertRequest.getConfirmPassword())) {
            throw BusinessException.from("登录密码与确认密码不一致，请重新输入");
        }
        
        if (this.getDao().selectCount(new QueryWrapper<TenantUserEntity>().eq("username", insertRequest.getUsername())) > 0) {
            throw BusinessException.from("登录名称重复，请更新其它登录名称");
        }
        insertRequest.setPassword(MD5Utils.md5Hex(insertRequest.getPassword()));
        
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public TenantUserEntity update(TenantUserUpdateRequest updateRequest) {
        
        TenantUserEntity tenantUserEntity = getDao().selectById(updateRequest.getId());
        tenantUserEntity.setName(updateRequest.getName());
        tenantUserEntity.setPhone(updateRequest.getPhone());
        tenantUserEntity.setEmail(updateRequest.getEmail());
        tenantUserEntity.setSortIndex(updateRequest.getSortIndex());
        tenantUserEntity.setRemark(updateRequest.getRemark());
        tenantUserEntity.setUpdateUser(ContextHolder.getContext().getUserId());
        tenantUserEntity.setUpdateTime(DateUtils.getNow());
        getDao().updateById(tenantUserEntity);
        
        tenantUserEntity.setPassword("******");
        return tenantUserEntity;
    }
    
    @Override
    public List<TenantUserEntity> search(TenantUserPageRequest pageRequest) {
        QueryWrapper<TenantUserEntity> tenantUserEntityQueryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isNotNull(pageRequest.getTenantId())) {
            tenantUserEntityQueryWrapper.eq("tenant_id", pageRequest.getTenantId());
        }
        tenantUserEntityQueryWrapper.orderByAsc("sort_index");
        return getDao().selectList(tenantUserEntityQueryWrapper);
    }
    
}
